﻿/**
 * Jean-François Desgagné 2009 Portfolio
 * Copyright (c) 2009 Jean-François Desgagné <jfesgagne@gmail.com>
 * 
 * This work is licensed under a Creative Commons Attribution 3.0 United States License.
 * Some Rights Reserved.
 */
package com.jfdesgagne.jfdesgagne09.modules.showcase.model {
	import com.jfdesgagne.jfdesgagne09.modules.showcase.model.vo.ProjectVO;
    import com.jfdesgagne.jfdesgagne09.modules.showcase.ApplicationFacade;
	import com.jfdesgagne.jfdesgagne09.shell.model.ConfigProxy;
	import com.jfdesgagne.jfdesgagne09.shell.model.LocaleProxy;
	import com.jfdesgagne.jfdesgagne09.modules.showcase.model.vo.ProjectVO;
	import flash.net.*;
    import org.puremvc.as3.multicore.interfaces.IProxy;
    import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	import flash.events.NetStatusEvent;

    public class ProjectsProxy extends Proxy implements IProxy {
        public static const NAME:String = "ProjectsProxy";
		private var _remotingService:NetConnection;
		private var _remotingResponder:Responder;
		public var projects:Array;
      	
        public function ProjectsProxy() {
        	super(NAME, new Object());
		}
		
		public static function getInstance():ProjectsProxy {
			return ApplicationFacade.getInstance(ApplicationFacade.NAME).retrieveProxy(ProjectsProxy.NAME ) as ProjectsProxy;
		}
		
		override public function onRegister():void {
			registerClassAlias("com.jfdesgagne.jfdesgagne09.modules.showcase.model.vo.ProjectVO", ProjectVO) ;
			this._remotingResponder = new Responder(this._receiveDataHandler, this._errorDataHandler);
			this._remotingService   = new NetConnection();
			this._remotingService.objectEncoding = ObjectEncoding.AMF3;
			this._remotingService.addEventListener(NetStatusEvent.NET_STATUS, this._netStatusHandler);
			this._remotingService.connect(ConfigProxy.getInstance().parameters.http_address + ConfigProxy.getInstance().config.website.remoting_gateway);
			this._remotingService.call("com.jfdesgagne09.Projects.getProjects",this._remotingResponder,LocaleProxy.lang);
		}
		
		private function _netStatusHandler($event:NetStatusEvent):void {
			for(var $element:String in $event.info) {
				trace($element+":"+$event.info[$element]);
			}
		}
		
		private function _receiveDataHandler($projects:Array):void {
			this.projects = $projects;
			sendNotification(ApplicationFacade.INITIALIZE);
			/*
			this.projects = new Array();
			for (var $i = 0; $i < $projects.length; $i++) {
				//this.projects.push(new ProjectVO($projects[$i].title, $projects[$i].task, $projects[$i].eye, $projects[$i].address, $projects[$i].name, $projects[$i].date));
			}
			var $project:ProjectVO = $projects[0] as ProjectVO;
			
			trace($projects[0].title);
			*/
		}
		
		private function _errorDataHandler($data:Object):void {
			throw new Error($data.description+"\n"+
				"Level: "+$data.level +"\n"+
				"Line : "+$data.line  +"\n"+
				"Code : " + $data.code  +"\n"
			);
		}
    }
}